﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using SS4U.Library.ConfigurationClasses;
using umbraco;
using umbraco.uicontrols;

namespace SS4U.Section.Configuration
{
    public partial class EditDocumentType : System.Web.UI.Page
    {
        public TabPage FirstTab;
        private ConfigurationRoot configRoot;
        private SearchDefinition searchDefinition;
        private DocumentType documentType;

        protected void Page_Load(object sender, EventArgs e)
        {
            string docTypeId = Request["id"];
            string definitionId = Request["definition"];
            configRoot = Library.Configuration.Root;
            searchDefinition = configRoot.SearchDefinitions.GetByName(definitionId);
            documentType =
                (from x in searchDefinition.DocumentTypes where x.Alias == docTypeId select x).FirstOrDefault();

            if (!IsPostBack)
            {

                lblName.Text = documentType.Alias;
                txtCategory.Text = documentType.Category;
                chkIncludeChildnodes.Checked = documentType.IncludeChildNodes;
                chkTestNodeName.Checked = documentType.TestNodeName;
            }

        }

        protected override void OnInit(EventArgs e)
        {
            // Tab setup
            FirstTab = tabControl.NewTabPage("Document Type");
            FirstTab.Controls.Add(Pane1);

            ////save button
            ImageButton save = FirstTab.Menu.NewImageButton();
            save.Click += save_click;
            save.AlternateText = "Save";
            save.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";

            //cboIterationType = new DropDownList();
            //txtName = new TextBox();
            //chkIsDefault = new CheckBox();
        }

        private void save_click(object sender, ImageClickEventArgs e)
        {
            documentType.Category = txtCategory.Text;
            documentType.IncludeChildNodes = chkIncludeChildnodes.Checked;
            documentType.TestNodeName = chkTestNodeName.Checked;

            Library.Configuration.SaveConfiguration();
        }
    }
}
